home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-68k-src / machines / amiga68k / libsrc / stdio / fread.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  2KB  |  58 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <proto/dos.h>
  5.  
  6. size_t fread(void *ptr,size_t size,size_t nmemb,FILE *f)
  7. {
  8.     size_t cnt,total=size*nmemb;
  9.     char *p=ptr;
  10.     long result;
  11.     if(!f||!total) return(0);
  12.     if((f->flags&(_READABLE|_WRITE|_ERR|_EOF))!=_READABLE) return(0);
  13.     f->flags|=_READ;
  14.     if(!f->bufsize){if(f->flags&_UNBUF) f->bufsize=1; else f->bufsize=BUFSIZ;}
  15.     if(!f->base){
  16.         if(!(f->base=(char *)malloc(f->bufsize+1)+1))
  17.             return 0;
  18.         f->pointer=f->base;
  19.         f->count=0;
  20.     }
  21.     if(cnt=f->count){
  22.     /*  Buffer lesen    */
  23.         if(total<=cnt){
  24.             memcpy(p,f->pointer,total);
  25.             f->pointer+=total;f->count-=total;
  26.             return(nmemb);
  27.         }else{
  28.             memcpy(p,f->pointer,cnt);
  29.             total-=cnt;p+=cnt;
  30.             f->count=0;f->pointer=f->base;
  31.         }
  32.     }
  33.     if(total<f->bufsize){
  34.         *p++=_fillbuf(f);
  35.         if(f->flags&_EOF) return cnt/size;
  36.         total--;cnt++;
  37.         if(f->count>=total){
  38.             memcpy(p,f->pointer,total);
  39.             f->pointer+=total;
  40.             f->count-=total;
  41.             return nmemb;
  42.         }else{
  43.             memcpy(p,f->pointer,f->count);
  44.             cnt+=f->count;f->count=0;
  45.             return cnt/size;
  46.         }
  47.     }else{
  48.         result=Read((BPTR)f->filehandle,p,total);
  49.         if(result==-1){f->flags|=_ERR;return(cnt/size);}
  50.         if(result<total){
  51.             f->flags|=_EOF;
  52.             return((cnt+result)/size);
  53.         }else{
  54.             return(nmemb);
  55.         }
  56.     }
  57. }
  58.